<?php

/**

 * 我的余额

 *

 *

 *

 *

 * @copyright  Copyright (c) 2007-2015 ShopNC Inc. (http://www.shopnc.net)

 * @license    http://www.shopnc.net

 * @link       http://www.shopnc.net

 * @since      File available since Release v1.1

 */







defined('ByShopWWI') or exit('Access Invalid!');



class member_fundControl extends mobileMemberControl {



    public function __construct(){

        parent::__construct();

    }

	

	

	/**

     * 充值列表

     */

    public function indexOp(){

        $condition = array();

        $condition['pdr_member_id'] = $this->member_info['member_id'];

        if (!empty($_GET['pdr_sn'])) {

            $condition['pdr_sn'] = $_GET['pdr_sn'];

        }



        $model_pd = Model('predeposit');

        $list = $model_pd->getPdRechargeList($condition,20,'*','pdr_id desc');

		foreach($list as $key=>$value){

			$list[$key]['pdr_add_time_text'] = date('Y-m-d H:i:s',$value['pdr_add_time']);

		}

		$page_count = $model_pd->gettotalpage();

        output_data(array('list' => $list),mobile_page($page_count));

    }

    /**

     * 余额记录

     */

    public function predepositlogOp() {

		$model_pd = Model('predeposit');

		$page = new Page();

        $condition = array();

        $condition['lg_member_id'] = $this->member_info['member_id'];

        $list = $model_pd->getPdLogList($condition,20,'*','lg_id desc');
		
			if (!$list) {
			$list = array();
		}
		if (!empty($list)) {

			foreach ($list as $key => $value) {

				$list[$key]['lg_add_time_text'] = date('Y-m-d H:i:s', $value['lg_add_time']);

			}

		}
		
	//	$list[$key]['lg_add_time_text'] = date('Y-m-d H:i:s', $value['lg_add_time']);

		$page_count = $model_pd->gettotalpage();

        output_data(array('list' => $list),mobile_page($page_count));

    }
    /**

     * 在线充值

     */

    public function pdrecharge_authOp() {
	
            $obj_validate = new Validate();
            $pdc_amount = abs(floatval($_POST['pdc_amount']));
            $validate_arr[] = array("input"=>$pdc_amount, "require"=>"true",'validator'=>'Compare','operator'=>'>=',"to"=>'0.01',"message"=>Language::get('predeposit_cash_add_pricemin_error'));
            $validate_arr[] = array("input"=>$_POST["pdc_bank_name"], "require"=>"true","message"=>Language::get('predeposit_cash_add_shoukuanbanknull_error'));
            $validate_arr[] = array("input"=>$_POST["pdc_bank_no"], "require"=>"true","message"=>Language::get('predeposit_cash_add_shoukuanaccountnull_error'));
            $validate_arr[] = array("input"=>$_POST["pdc_bank_user"], "require"=>"true","message"=>Language::get('predeposit_cash_add_shoukuannamenull_error'));
            $validate_arr[] = array("input"=>$_POST["password"], "require"=>"true","message"=>'请输入支付密码');
            $obj_validate -> validateparam = $validate_arr;
            $error = $obj_validate->validate();
            if ($error != ''){
                output_error($error);
            }
            $model_pd = Model('predeposit');
            $model_member = Model('member');
            $member_info = $model_member->getMemberInfoByID($this->member_info['member_id']);
            //验证支付密码
            if (md5($_POST['password']) != $member_info['member_paypwd']) {
                output_error('支付密码错误');
            }
            //验证金额是否足够
            if (floatval($member_info['available_predeposit']) < ($pdc_amount)){
                output_error(Language::get('predeposit_cash_shortprice_error'));
            }



            try {
                $model_pd->beginTransaction();
                $pdc_sn = $model_pd->makeSn();
                $data = array();
                $data['pdc_sn'] = $pdc_sn;
                $data['pdc_member_id'] = $this->member_info['member_id'];
                $data['pdc_member_name'] = $this->member_info['member_name'];
                $data['pdc_amount'] = $pdc_amount;
                $data['pdc_bank_name'] = $_POST['pdc_bank_name'];
                $data['pdc_bank_no'] = $_POST['pdc_bank_no'];
                $data['pdc_bank_user'] = $_POST['pdc_bank_user'];
                $data['pdc_add_time'] = TIMESTAMP;
                $data['pdc_payment_state'] = 0;
				 $list_setting = Model('setting')->getListSetting();
				$data['pdc_amount_fee']=$pdc_amount*$list_setting['site_shuifei']/100;//手续费
				//exit(print_r($data));
                $insert = $model_pd->addPdCash($data);
                if (!$insert) {
                    throw new Exception(Language::get('predeposit_cash_add_fail'));
                }
                //冻结可用预存款
                $data = array();
                $data['member_id'] = $member_info['member_id'];
                $data['member_name'] = $member_info['member_name'];
                $data['amount'] = $pdc_amount;
                $data['order_sn'] = $pdc_sn;
                $model_pd->changePd('cash_apply',$data);
                $model_pd->commit();
                output_data(Language::get('predeposit_cash_add_success'));
            } catch (Exception $e) {
                $model_pd->rollback();
                output_data($e->getMessage());
            }


	
	}
    /**

     * 在线充值

     */

    public function pdrecharge_addOp() {

		if($this->member_info['member_id']){
			$pdr_amount = abs(floatval($_POST['pdr_amount']));
			if ($pdr_amount <= 0) {
				showMessage(Language::get('predeposit_recharge_add_pricemin_error'),'','html','error');
			}
	
			$model_pdr = Model('predeposit');
			$data = array();
			$data['pdr_sn'] = $pay_sn = $model_pdr->makeSn();
			$data['pdr_member_id'] = $this->member_info['member_id'];
			$data['pdr_member_name'] = $this->member_info['member_name'];
			$data['pdr_amount'] = $pdr_amount;
			$data['pdr_add_time'] = TIMESTAMP;
			$insert = $model_pdr->addPdRecharge($data);
			if ($insert) {
			 output_data(array('pay_sn' => $pay_sn));
				//转向到商城支付页面
				//redirect(SHOP_SITE_URL . '/index.php?act=buy&op=pd_pay&pay_sn='.$pay_sn.'&upg='.$_POST['upg'].'&kucun='.$_POST['kucun'].'&paymoney='.$pdr_amount);
			}
		}else{
		  output_error('请登录！');
		} 

	    //output_data(array('list' => $list),mobile_page($page_count));

    }



	/**

     * 体现记录

     */

    public function pdcashlistOp() {

		$condition = array();

        $condition['pdc_member_id'] =  $this->member_info['member_id'];

        if (preg_match('/^\d+$/',$_GET['sn_search'])) {

            $condition['pdc_sn'] = $_GET['sn_search'];

        }

        if (isset($_GET['paystate_search'])){

            $condition['pdc_payment_state'] = intval($_GET['paystate_search']);

        }

        $model_pd = Model('predeposit');

        $cash_list = $model_pd->getPdCashList($condition,30,'*','pdc_id desc');

			if (!$cash_list) {
			$cash_list = array();
		}
		if (!empty($cash_list)) {

			foreach ($cash_list as $key => $value) {
				
				if ($value['pdc_payment_state']==1) {
				$cash_list[$key]['pdc_payment_state_text'] = '已审核';
				}else{
				$cash_list[$key]['pdc_payment_state_text'] = '待审核';
				}
				
                $cash_list[$key]['pdc_add_time_text'] = date('Y-m-d H:i:s', $value['pdc_add_time']);
				
                if (!empty($value['pdc_payment_time'])) {
					
				$cash_list[$key]['pdc_payment_time_text'] = date('Y-m-d H:i:s', $value['pdc_payment_time']);
				
				}
			}

		}



		$page_count = $model_pd->gettotalpage();

        output_data(array('list' => $cash_list),mobile_page($page_count));



	}

	





	/**

     * 充值卡充值

     */

	public function rechargecard_addOp() {

		if($this->member_info['member_id']){

            if(!$this->check()){

				output_error('验证码错误！');

			}

       



			$sn = (string) $_POST['rc_sn'];

			if (!$sn || strlen($sn) > 50) {

				output_error('平台充值卡卡号不能为空且长度不能大于50');

				exit;

			}



			try {

				model('predeposit')->addRechargeCard($sn, $this->member_info);

				output_data('平台充值卡使用成功');

			} catch (Exception $e) {

				output_error($e->getMessage());

				exit;

			}

		}else{

			output_error('请登录！');

		} 

	}

		



		

	/**

     * 充值卡记录列表

     */

	public function rcblogOp() {

		$model = Model();

        $list = $model->table('rcb_log')->where(array('member_id' => $this->member_info['member_id']))->page(20)->order('id desc')->select();



		$page_count = $model->gettotalpage();

        output_data(array('log_list' => $list),mobile_page($page_count));

	}



	/**

     * 我的积分 我的余额

     */

    public function my_assetOp() {

		$point = $this->member_info['member_points'];

		output_data(array('point' => $point));

	}

	protected function getMemberAndGradeInfo($is_return = false){

        $member_info = array();

        //会员详情及会员级别处理

        if($this->member_info['member_id']) {

            $model_member = Model('member');

            $member_info = $model_member->getMemberInfoByID($this->member_info['member_id']);

            if ($member_info){

                $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_info['member_exppoints']));

                $member_info = array_merge($member_info,$member_gradeinfo);

                $member_info['security_level'] = $model_member->getMemberSecurityLevel($member_info);

            }

        }

        if ($is_return == true){//返回会员信息

            return $member_info;

        } else {//输出会员信息

            Tpl::output('member_info',$member_info);

        }

    }

	

	/**

     * AJAX验证

     *

     */

	protected function check(){

        if (checkSeccode($_POST['nchash'],$_POST['captcha'])){

            return true;

        }else{

            return false;

        }

    }

}

